package com.supermap.dataservice.util;

/**
 * 编码转换
 * @Author:zhoujian
 * @Desciption:
 * @date:2019-04-03 10:35
 * @version:v1.0.0
 */

public class EncodeUtils {

    /**
     * gb2312解码
     * @param string 需要解码字符串
     * @return
     * @throws Exception
     */
    public static String gb2312decode( String string) throws Exception
    {
        byte[] bytes = new byte[string.length() / 2];
        for(int i = 0; i < bytes.length; i ++){
            byte high = Byte.parseByte(string.substring(i * 2, i * 2 + 1), 16);
            byte low = Byte.parseByte(string.substring(i * 2 + 1, i * 2 + 2), 16);
            bytes[i] = (byte) (high << 4 | low);
        }
        return new String(bytes, "gb2312");
    }

    /**
     * 中文转化通过gb2312转码
     * @param string 需要转码字符串
     * @return
     * @throws Exception
     */
    public static String gb2312eecode(String string) throws Exception
    {
        StringBuffer gbkStr = new StringBuffer();
        byte[] gbkDecode = string.getBytes("gb2312");
        for (byte b : gbkDecode) {
            gbkStr.append("%");
            gbkStr.append(Integer.toHexString(b & 0xFF).toUpperCase());
//				System.out.println(Integer.toHexString(b & 0xFF));
        }
        return gbkStr.toString();//.substring(0, gbkStr.lastIndexOf("%"));
    }
}